﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entity;
using System.Data.Entity;
namespace WCFRestfulService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service.svc or Service.svc.cs at the Solution Explorer and start debugging.
    public class Service : IService
    {
        private WCFDBEntities dbEntity = new WCFDBEntities();
        public ReturnJsonInfo<List<UserInfo>> GetAllUsers()
        {
            ReturnJsonInfo<List<UserInfo>> returnJson = new ReturnJsonInfo<List<UserInfo>>();
            try
            {
                List<UserInfo> userList = new List<UserInfo>();
                userList = dbEntity.UserInfoes.ToList();
                if (userList.Count>0)
                {
                    returnJson.status = 1;
                    returnJson.message = "get user info success";
                    returnJson.data = userList;
                }
                else
                {
                    returnJson.status = 1;
                    returnJson.message = "no user data";
                    returnJson.data = null;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = "fail to get user info:"+error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        public ReturnJsonInfo<UserInfo> GetUser(string id)
        {
            ReturnJsonInfo<UserInfo> returnJson = new ReturnJsonInfo<UserInfo>();
            if (string.IsNullOrEmpty(id))
            {
                returnJson.status = 0;
                returnJson.message = "id is invalid";
                returnJson.data = null;
            }
            else
            {
                try
                {
                    int index = Convert.ToInt32(id);
                    UserInfo user = dbEntity.UserInfoes.FirstOrDefault(t => t.Id == index);
                    returnJson.status = 1;
                    returnJson.message = user == null ? "no user info" : "get user info success";
                    returnJson.data = user;
                }
                catch (Exception error)
                {
                    returnJson.status = 0;
                    returnJson.message = "fail to get data:"+error.Message;
                    returnJson.data = null;
                }
            }
            return returnJson;
        }

        public ReturnJsonInfo<int> AddUser(UserInfo user)
        {
            ReturnJsonInfo<int> returnJson = new ReturnJsonInfo<int>();
            try
            {
                dbEntity.Entry<UserInfo>(user).State = EntityState.Added;
                dbEntity.SaveChanges();
                returnJson.status = 1;
                returnJson.message = "insert data success";
                returnJson.data = user.Id;
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = "insert data fail:"+error.Message;
                returnJson.data = 0;
            }
            return returnJson;
        }

        public ReturnJsonInfo<UserInfo> UpdateUser(UserInfo user)
        {
            ReturnJsonInfo<UserInfo> returnJson = new ReturnJsonInfo<UserInfo>();
            try
            {
                dbEntity.Entry<UserInfo>(user).State = EntityState.Modified;
                dbEntity.SaveChanges();
                returnJson.status = 1;
                returnJson.message = "update data success";
                returnJson.data = user;
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = "update data fail:" + error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        public ReturnJsonInfo<int> DeleteUser(string id)
        {
            ReturnJsonInfo<int> returnJson = new ReturnJsonInfo<int>();
            try
            {
                int index = Convert.ToInt16(id);
                UserInfo user = dbEntity.UserInfoes.FirstOrDefault(t => t.Id == index);
                dbEntity.Entry<UserInfo>(user).State = EntityState.Deleted;
                int flag=dbEntity.SaveChanges();
                returnJson.status = 1;
                returnJson.message = "delete data success";
                returnJson.data = flag;
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = "update data fail:" + error.Message;
                returnJson.data = 0;
            }
            return returnJson;
        }

    }
}
